//
//  ButtonAndTableView.m
//  SZBank
//
//  Created by LuoYao on 16/7/8.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "ButtonAndTableView.h"

@interface ButtonAndTableView ()
{
    CGFloat _yWidth,_yHeight,_cw,_ch;
    CGRect selfFrame;
    UIView *vi;/**< 遮罩层 */
}
@property (nonatomic) UIButton *titleBtn;/**< 头部Btn */
@property (nonatomic) UITableView *contentTableView;/**< 正文table */
@property (nonatomic) NSArray *dataArray;/**< 数组，用于存储传过来的显示清单dic[@"list"]的Value */
@end


@implementation ButtonAndTableView

- (instancetype)initWithFrame:(CGRect)frame dataSource:(NSDictionary *)dataSource backImageName:(NSString *)imageName selectImageName:(NSString *)selectImageName
{
    self = [super initWithFrame:frame];
    if (self) {
        _yHeight = frame.size.height;
        _yWidth = frame.size.width;
        
        self.dataArray = dataSource[@"list"];
        
        
        //配置button
        [self.titleBtn setTitle:dataSource[@"title"] forState:UIControlStateNormal];
        [self.titleBtn setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:imageName] forState:UIControlStateNormal];
        [self.titleBtn setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:selectImageName] forState:UIControlStateSelected];
        self.titleBtn.backgroundColor = [UIColor clearColor];
        [self addSubview:self.titleBtn];
        
    }
    return self;
}

#pragma mark - BtnClick
- (void)action_BtnClick:(UIButton *)sender{
    
    
    //测试,新建一个View作为遮罩
    vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
    vi.backgroundColor = [UIColor clearColor];

    [self.superview addSubview:vi];
    
    
    //添加手势
    // 单击
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                         initWithTarget:self
                                         action:@selector(action_TapGesture)];
    singleTap.numberOfTapsRequired = 1;
    [vi addGestureRecognizer:singleTap];
    
    
    if (sender.selected) {
        [self removeAnimatonAction];
        sender.selected = NO;
        return;
    }
    
    
    CGFloat tableViewHeight = _yHeight * self.dataArray.count;
    if (tableViewHeight > KHEIGHT / 2) {
        tableViewHeight = KHEIGHT / 2;
    }
    
    [self.contentTableView setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y - 50, _yWidth, tableViewHeight)];
    
    //动画效果
    [UIView animateWithDuration:0.2 animations:^{
        self.contentTableView.frame = CGRectMake(self.frame.origin.x, CGRectGetMaxY(self.frame), _yWidth, tableViewHeight);
    }];
    [self.superview addSubview:self.contentTableView];
    
    
    sender.selected = YES;
    
    
}

#pragma mark - UItableViewDeleGate && dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell" forIndexPath:indexPath];
    cell.textLabel.text = self.dataArray[indexPath.row];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //为协议方法赋值
    if (self.delegate) {
        [_delegate btnTableView:self didSelectRowAtIndexPath:indexPath selectTitle:self.dataArray[indexPath.row]];
    }
    //文本赋值
    [self.titleBtn setTitle:self.dataArray[indexPath.row] forState:UIControlStateNormal];
    
    //改变按钮状态
    self.titleBtn.selected = !self.titleBtn.selected;
    //从父视图移除
    [self removeAnimatonAction];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _yHeight;
}


#pragma mark - removeSelf

- (void)removeAnimatonAction{
    [UIView animateWithDuration:0.2 animations:^{
        self.contentTableView.center = CGPointMake(self.center.x, self.frame.origin.y);
    } completion:^(BOOL finished) {
        [self.contentTableView removeFromSuperview];
        [vi removeFromSuperview];
    }];
}

#pragma mark - 手势执行方法
- (void)action_TapGesture{
    self.titleBtn.selected = !self.titleBtn.selected;
    [self removeAnimatonAction];
}


#pragma mark - getter
- (UIButton *)titleBtn{
    if (!_titleBtn) {
        _titleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, _yWidth, _yHeight)];
        _titleBtn.titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE + 2];
        _titleBtn.backgroundColor = [UIColor colorWithRed:0.952 green:0.952 blue:0.952 alpha:1.0];
        [_titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_titleBtn addTarget:self action:@selector(action_BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return _titleBtn;
}


- (UITableView *)contentTableView{
    if (!_contentTableView) {
        _contentTableView = [[UITableView alloc] init];
        _contentTableView.dataSource = self;
        _contentTableView.delegate = self;
        
        _contentTableView.backgroundColor = [UIColor whiteColor];
        _contentTableView.tableFooterView = [[UIView alloc] init];

        
        [_contentTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableCell"];
    }
    return _contentTableView;
}






@end
